/**
 * Created by sxwu on 14-2-15.
 */



/**
 * 模块依赖
 */
var db = require("../lib/db");


//查找记录
exports.selectForProxy = function(userid, handler) {
    db.execQuery({
        "sql": "SELECT a.*,b.username,b.nickname FROM virwifiinfo a, viruser b " +
            " WHERE a.userid in (select id from viruser where proxyid=?) and a.userid = b.id",
        "args": [userid],
        "handler": handler
    });
};
//查找记录
exports.selectByUserID = function(userid, handler) {
    db.execQuery({
        "sql": "SELECT a.*,b.username,b.nickname FROM virwifiinfo a, viruser b WHERE a.userid in (?) and a.userid = b.id",
        "args": [userid],
        "handler": handler
    });
};

exports.selectByWiFiID = function(id, handler) {
    db.execQuery({
        "sql": "SELECT * FROM virwifiinfo WHERE id=?",
        "args": [id],
        "handler": handler
    });
};

//遍历记录
exports.selectAll = function(handler) {
    db.execQuery({
        "sql": "SELECT a.*,b.username,b.nickname FROM virwifiinfo a, viruser b where a.userid=b.id ORDER BY b.id asc",
        "handler": handler
    });
};



exports.deleteByID = function(id, handler) {
    db.execQuery({
        "sql": "delete FROM virwifiinfo WHERE id=?",
        "args": [id],
        "handler": handler
    });
};

//插入记录
exports.insert = function(wifiinfo, handler) {
    db.execQuery({
        "sql": "insert into virwifiinfo(userid,wifissid,wifiauthtype,wifiauthhour,wifiauthbrand,wifiredirecturl,wifipassdomain,weixincode) " +
            "values(?,?,?,?,?,?,?,?)",
        "args":[ wifiinfo.userid,
            wifiinfo.wifissid,
            wifiinfo.wifiauthtype,
            wifiinfo.wifiauthhour,
            wifiinfo.wifiauthbrand,
            wifiinfo.wifiredirecturl,
            wifiinfo.wifipassdomain,
            wifiinfo.weixincode],
        "handler": handler
    });
};

//更新记录
exports.update = function(wifiinfo, handler) {
    db.execQuery({
        "sql": "update virwifiinfo set wifissid=?,wifiauthtype=?,wifiauthhour=?," +
            "wifiauthbrand=?,wifiredirecturl=?,weixincode=?,wifipassdomain=? WHERE id=?",
        "args": [wifiinfo.wifissid,
            wifiinfo.wifiauthtype,
            wifiinfo.wifiauthhour,
            wifiinfo.wifiauthbrand,
            wifiinfo.wifiredirecturl,
            wifiinfo.weixincode,
            wifiinfo.wifipassdomain,
            wifiinfo.id],
        "handler": handler
    });

};
///////////////////////////////////////////////////////




//遍历记录
exports.findAll = function(handler) {
    db.execQuery({
        "sql": "SELECT * FROM virwifiinfo ORDER BY id asc",
        "handler": handler
    });
};
